﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityModel;
using BusinessLayer;
using System.IO;
using System.Data.Linq;

namespace PL___Web_Application
{
    public partial class ViewCar : PagesMethods
    {
        private Car showcar;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string car_reg = Request.QueryString["id"];
                BL controller = new BL();
                showcar = controller.getCar(car_reg);
                regnumber_lb.Text = showcar.Vehicle.reg_number;
                fuel_lb.Text = controller.getFuelName((int)showcar.Vehicle.fuel);
                price_lb.Text = showcar.Vehicle.price.ToString();
                colour_lb.Text = showcar.Vehicle.colour;
                state_car_lb.Text = controller.getStateName((int)showcar.Vehicle.state);
                type_car_lb.Text =  controller.getCarTypeName((int)showcar.type_car);
                num_doors_lb.Text = showcar.doors_number.ToString();
                if (state_car_lb.Text == "Available")
                {
                    buy_button.Visible = true;                
                }
                car_img.ImageUrl = "Image.aspx?id=" + car_reg + "&type=car";    
            }
            catch (Exception ex) {
                error_lb.Text = ex.Message;
            }   
        }
        protected void BuyCar(object sender, EventArgs e)
        {
            try
            {
                CContract con = new CContract(showcar, GetPrivateFromCookie(), DateTime.Now);
                showcar.Vehicle.state = 2;                
                BL controller = new BL();
                controller.updateCar(showcar);
                controller.AddCustomerContract(con);
                String scriptText = "alert('Your purchase has been registered, our staff will contact you to request more data')";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", scriptText, true);
                Response.Redirect("PrivateAccount.aspx");
            }
            catch {
                String scriptText = "alert('Error registering the purchase, contact with our staff please')";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", scriptText, true);
            }

           
        }
       
    }
}